/* Copyright 2011 Gilberto Pacheco Gallegos.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.  */
package net.ramptors.dominiobd.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.event.shared.UmbrellaException;
import java.util.Collections;
import net.ramptors.dominiobd.client.grupos.AbccGrupos;
import net.ramptors.dominiobd.client.usuarios.AbccUsuarios;

/**
 * @author Gilberto Pacheco Gallegos
 */
public class DominioBd extends ResizeComposite {
  private static DominioBdUiBinder uiBinder = GWT.create(DominioBdUiBinder.class);
  private static final ServicioSesionAsync proxyServicioSesion =
      GWT.create(ServicioSesion.class);
  @UiField
  InlineLabel etiquetaUsuario;
  @UiField
  TabLayoutPanel tab;
  @UiField
  AbccUsuarios abccUsuarios;
  @UiField
  AbccGrupos abccGrupos;
  public DominioBd() {
    initWidget(uiBinder.createAndBindUi(this));
    proxyServicioSesion.getUsuario(new AsyncCallback<String>() {
      public void onFailure(final Throwable caught) {
        procesa(caught);
      }
      public void onSuccess(final String usuario) {
        etiquetaUsuario.setText(usuario);
      }
    });
  }
  @Override
  protected void onAttach() {
    super.onAttach();
    // Para que IE8 e IE9 muestren bien el primer tab.
    tab.forceLayout();
  }
  public void actualiza() {
    // Para que IE7 muestre bien el primer tab.
    Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
      @Override
      public void execute() {
        tab.forceLayout();
        abccUsuarios.actualiza();
      }
    });
  }
  @UiHandler("tab")
  void pestanaSeleccionada(final SelectionEvent<Integer> evento) {
    switch (evento.getSelectedItem()) {
    case 0:
      abccUsuarios.actualiza();
      break;
    case 1:
      abccGrupos.actualiza();
      break;
    }
  }
  private void procesa(Throwable caught) {
    throw new UmbrellaException(Collections.singleton(caught));
  }
  interface DominioBdUiBinder extends UiBinder<Widget, DominioBd> {
  }
}